package com.sztest.jayjun.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.sztest.jayjun.MyApplication;
import com.sztest.jayjun.constant.Constant;
import com.sztest.jayjun.dialog.HintDialog;
import com.sztest.jayjun.utils.LogUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.ViewDataBinding;
import pri.tool.pump.PumpInterface;

/*
 *  @项目名：  jayjun
 *  @包名：    com.sztest.jayjun.base
 *  @文件名:   BaseActivity
 *  @创建者:   okmin
 *  @创建时间:  2022/6/27 16:07
 *  @描述：    TODO
 */
public abstract class BaseActivity extends AppCompatActivity {
    ViewDataBinding mDataBinding;
    protected Context mContext;
    protected boolean keydownFlag;
    private HintDialog hintDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        setSystemUIVisible(false);
        mContext=this;
        setDataBinding();
        initView();
        initData();
    }

    protected abstract void setDataBinding();

    protected abstract void initView();

    protected abstract void initData();


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        setSystemUIVisible(false);
    }

    private void setSystemUIVisible(boolean show) {
//        hideBottomUIMenu();
//        if (show) {
//            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//            uiFlags |= 0x00001000;
//            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
//        } else {
//            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
//            uiFlags |= 0x00001000;
//            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
//        }
    }


    /**
     * 关机
     */
    public void sysShutdown()
    {
        LogUtils.e("关机！！！");
        Intent intent = new Intent("com.android.internal.intent.action.REQUEST_SHUTDOWN");
        intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        MyApplication.getInstance().mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                MyApplication.getInstance().execRootCmd("reboot -p");
            }
        },5000);
    }

   protected Runnable mCloseSys=new Runnable() {
        @Override
        public void run() {
            sysShutdown();
        }
    };
/*
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.e("onKeyDown==="+keyCode);
        if (keydownFlag){
            return true;
        }
        keydownFlag = true;
        if (keyCode== Constant.FOOT) {
            LogUtils.e("f3===");
            return true;
        }*//*else if (keyCode==Constant.ALARM ) {
            return true;
        }*//*else if (keyCode== Constant.CLOSESYS) {
            LogUtils.e("131???");
            MyApplication.getInstance().mHandler.postDelayed(mCloseSys,200);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    // TODO: 2022/7/5 按键
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        LogUtils.e("onKeyUp==="+keyCode);
        keydownFlag = false;
        if (keyCode== Constant.FOOT) {
            LogUtils.e("f3===");
            return true;
        }*//*else if (keyCode==Constant.ALARM ) {
            return true;
        }*//*else if (keyCode== Constant.CLOSESYS) {
//            MyApplication.getInstance().mHandler.removeCallbacks(mCloseSys);
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }*/

    //  应用按键：键值： F1。 (F1=PL9            系统关机按键 )
    //   应用按键：键值： F2 。  (F2=PL11           ALARM报警)
    //  应用按键：键值： F3。  （F3=PE3            脚踏开关）




    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

}
